package com.ebt.m.wiki.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.ebt.m.R;

import java.util.List;

/**
 * BaseAdapter 抽象泛型类
 *
 * @param <T>
 * @author Michael.li
 */
public abstract class EbtBaseAdapter<T> extends BaseAdapter {
    protected int selectedIndex = -1;
    protected List<T> list;
    protected Context context;

    public EbtBaseAdapter(Context context, List<T> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public T getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void remove(T object) {
        list.remove(object);
    }

    public T getSelectedItem() {
        if (selectedIndex == -1) {
            return null;
        }
        return list.get(selectedIndex);
    }

    public void setData(List<T> list1) {
        this.list.clear();
        this.list.addAll(list1);
        notifyDataSetChanged();
    }

    public void replaceData(List<T> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public List<T> getList() {
        return list;
    }

    public void addData(List<T> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 设置选中的项目
     *
     * @param position
     */
    public void setSelectedIndex(int position) {
        this.selectedIndex = position;
        notifyDataSetChanged();
    }

    public void setSelectedIndexNoRefresh(int position) {
        this.selectedIndex = position;
    }

    /**
     * 设置选中，只改变背景色
     *
     * @param listView
     * @param position
     */
    public void setSelected(ListView listView, int position) {
        int first = listView.getFirstVisiblePosition();
        View itemView = listView.getChildAt(selectedIndex - first);
        if (itemView != null) {
            itemView.setBackgroundResource(R.color.full_transparent);
        }
        this.selectedIndex = position;
        itemView = listView.getChildAt(selectedIndex - first);
        if (itemView != null) {
            itemView.setBackgroundResource(R.color.list_item_focused);
        }
    }

    /**
     * 获得当前选中的索引
     *
     * @return
     */
    public int getSelectedIndex() {
        return this.selectedIndex;
    }

    /**
     * 获得当前itemView
     *
     * @param listview
     * @return
     */
    public ViewGroup getSelectedView(ListView listview) {
        return (ViewGroup) listview.getChildAt(selectedIndex - listview.getFirstVisiblePosition());
    }

    public ViewGroup getSelectedView(ListView listview, int position) {
        return (ViewGroup) listview.getChildAt(position - listview.getFirstVisiblePosition());
    }

    /**
     * 将当前选中项移动到指定位置并选中
     *
     * @param position
     */
    public void moveSelection(int position) {
        if (position > -1 && position < list.size()) {
            T obj = list.get(selectedIndex);
            list.remove(selectedIndex);
            list.add(position, obj);
        }
        selectedIndex = position;
        notifyDataSetChanged();
    }

    public abstract View getView(int position, View convertView, ViewGroup parent);

    protected View createListItemView(int resource, ViewGroup root) {
        LayoutInflater inflater = LayoutInflater.from(context);
        return inflater.inflate(resource, root, false);
    }
}
